#include <stdio.h>
#include <string.h>
#include <assert.h>

int judge_prime(int number)
{
	if(number <= 1)
	{
		return 0;
	}
	int i = 0;
	for(i = 2; i < number; ++i)
	{
		if(number % i == 0)
		{
			return 0;
		}
	}
	return number;
}

long number(int i)
{
	if(i <= 0)
	{
		return 0;
	}
	if(i == 1)
	{
		return i;
	}
	return i * number(i - 1);
}

void getMaxMin(int * array, int number)
{
	if(NULL == array || number <= 0)
	{
		return;
	}
	int *max = array;
	int *min = array;
	int i = 0;
	for(; i < number; ++i)
	{
		if(*max < *(array + i))
		{
			max = array + i;
		}
		if(*min > *(array + i))
		{
			min = array + i;
		}
	}
	printf("max:%d,min:%d\n", *max, *min);
}

int countWords(char* str)
{
	if(str  == NULL)
	{
		return 0;
	}
	char *ptr = strtok(str, " ");
	int count = 0;
	while(ptr)
	{
		if(!strcmp(ptr, "!"))
		{
			break;
		}
		++count;
		ptr = strtok(NULL, " ");
	}
	return count;
}

char *my_strcpy(char * dest, const char* src)
{
	assert(dest != NULL && src != NULL);
	char * temp = dest;
	int count = strlen(src) + 1;
	if(dest >= src && dest <= src + count -1)
	{
		temp = temp + count -1;
		src = src + count -1;
		while(count--)
		{
			*temp-- = *src--;
		}
	}
	else
		while((*temp++ = *src++) != '\0');
	return dest;
}

char * my_strcat(char *dest, const char *src)
{
	assert(dest != NULL && src != NULL);
	char *ptr = dest;
	while(*ptr++ != '\0');
	ptr = ptr -1;
	while((*ptr++ = *src++) != '\0');
	return dest;
}

int main(int argc, char **argv)
{
	int i = 0;
	double sum = 0;
	for(i = 2; i <= 1000; ++i)
	{
		sum += judge_prime(i);
	}
	printf("%.0lf\n", sum);
	printf("%ld\n", number(6));
	int array[7] = {3,2,1,5,6,7,9};
	getMaxMin(array, 7);
	char str[100] = "hello world !";
	printf("hello world ! words count:%d\n", countWords(str));
	printf("%s count:%lu\n", str+2, strlen(str));
	printf("%s\n",my_strcpy(str+2, str));
	printf("my_strcat %s\n",my_strcat(str+2, "world"));
    struct {
        int a;
        int b;
        char arry[0];
    } stest;
    printf("------------%lu\n", sizeof(stest));

	return 0;
}
